package com.swgk.core.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.ColorInt;
import androidx.annotation.StringRes;
import androidx.databinding.DataBindingUtil;

import com.swgk.core.R;
import com.swgk.core.databinding.DialogRoundBinding;


/**
 * 圆角提示弹窗Dialog
 */
public class RoundDialog extends Dialog implements View.OnClickListener {
    private DialogRoundBinding binding;
    private RoundDialogEntity mEntity;

    private RoundDialog(Context context, RoundDialogEntity entity) {
        super(context, R.style.RoundCornerDialog);
        mEntity = entity;
        if (mEntity == null) {
            mEntity = new RoundDialogEntity();
        }
        //
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.dialog_round, null, false);
        setContentView(binding.getRoot());
        init();
        //
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.gravity = Gravity.CENTER;
            window.setAttributes(params);
        }
    }

    private void init() {
        initView();
        initEvent();
    }

    private void initView() {
        if (!TextUtils.isEmpty(mEntity.getTitle())) {
            binding.tvTitle.setVisibility(View.VISIBLE);
            binding.tvTitle.setText(mEntity.getTitle());
        }
        if (!TextUtils.isEmpty(mEntity.getContent())) {
            binding.tvMessage.setText(mEntity.getContent());
            binding.tvMessage.setGravity(mEntity.getContentGravity());
        }
        if (!TextUtils.isEmpty(mEntity.getCancel())) {
            binding.tvCancel.setText(mEntity.getCancel());
        }
        if (!TextUtils.isEmpty(mEntity.getConfirm())) {
            binding.tvConfirm.setText(mEntity.getConfirm());
        }
        if (mEntity.getCancelColor() != -1) {
            binding.tvCancel.setTextColor(mEntity.getCancelColor());
        }
        if (mEntity.getConfirmColor() != -1) {
            binding.tvConfirm.setTextColor(mEntity.getConfirmColor());
        }
        if (mEntity.isSingle()) {
            binding.tvCancel.setVisibility(View.GONE);
        } else {
            binding.tvCancel.setVisibility(View.VISIBLE);
        }
    }

    private void initEvent() {
        binding.tvCancel.setOnClickListener(this);
        binding.tvConfirm.setOnClickListener(this);
        //
        setCancelable(mEntity.isCancelable());
        //
        if (mEntity.getDismissListener() != null) {
            setOnDismissListener(mEntity.getDismissListener());
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_cancel) {
            if (mEntity.getCancelCallback() != null) {
                mEntity.getCancelCallback().onClick(v);
            }
            dismiss();
        } else if (id == R.id.tv_confirm) {
            if (mEntity.getConfirmCallback() != null) {
                mEntity.getConfirmCallback().onClick(v);
            }
            dismiss();
        }
    }

    public static class Builder {
        private Context context;
        private RoundDialogEntity entity;

        public Builder(Context context) {
            this.context = context;
            entity = new RoundDialogEntity();
        }

        public Builder title(String title) {
            entity.setTitle(title);
            return this;
        }

        public Builder title(@StringRes int titleRes) {
            entity.setTitle(context.getString(titleRes));
            return this;
        }

        public Builder content(String content) {
            entity.setContent(content);
            return this;
        }

        public Builder content(@StringRes int contentRes) {
            entity.setContent(context.getString(contentRes));
            return this;
        }

        public Builder content(CharSequence content) {
            entity.setContent(content);
            return this;
        }

        public Builder contentGravity(int gravity) {
            entity.setContentGravity(gravity);
            return this;
        }

        public Builder cancel(String cancel) {
            entity.setCancel(cancel);
            return this;
        }

        public Builder cancel(@StringRes int cancelRes) {
            entity.setCancel(context.getString(cancelRes));
            return this;
        }

        public Builder cancelColor(@ColorInt int color) {
            entity.setCancelColor(color);
            return this;
        }

        public Builder confirm(String confirm) {
            entity.setConfirm(confirm);
            return this;
        }

        public Builder confirm(@StringRes int confirmRes) {
            entity.setConfirm(context.getString(confirmRes));
            return this;
        }

        public Builder confirmColor(@ColorInt int color) {
            entity.setConfirmColor(color);
            return this;
        }

        public Builder cancelCallback(SingleCallback callback) {
            entity.setCancelCallback(callback);
            return this;
        }

        public Builder confirmCallback(SingleCallback callback) {
            entity.setConfirmCallback(callback);
            return this;
        }

        public Builder dismissListener(OnDismissListener dismissListener) {
            entity.setDismissListener(dismissListener);
            return this;
        }

        public Builder singleBtn(boolean isSingle) {
            entity.setSingle(isSingle);
            return this;
        }

        public Builder cancelable(boolean cancelable) {
            entity.setCancelable(cancelable);
            return this;
        }

        public RoundDialog build() {
            return new RoundDialog(context, entity);
        }

        public void show() {
            new RoundDialog(context, entity).show();
        }
    }

    public static class RoundDialogEntity {
        private String title;
        private CharSequence content;
        private int contentGravity = Gravity.CENTER;
        private String cancel;
        @ColorInt
        private int cancelColor = -1;
        private String confirm;
        @ColorInt
        private int confirmColor = -1;
        private boolean isSingle;
        private boolean cancelable = true;
        private SingleCallback cancelCallback;
        private SingleCallback confirmCallback;
        private OnDismissListener dismissListener;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public CharSequence getContent() {
            return content;
        }

        public void setContent(CharSequence content) {
            this.content = content;
        }

        public int getContentGravity() {
            return contentGravity;
        }

        public void setContentGravity(int contentGravity) {
            this.contentGravity = contentGravity;
        }

        public String getCancel() {
            return cancel;
        }

        public void setCancel(String cancel) {
            this.cancel = cancel;
        }

        public int getCancelColor() {
            return cancelColor;
        }

        public void setCancelColor(int cancelColor) {
            this.cancelColor = cancelColor;
        }

        public String getConfirm() {
            return confirm;
        }

        public void setConfirm(String confirm) {
            this.confirm = confirm;
        }

        public int getConfirmColor() {
            return confirmColor;
        }

        public void setConfirmColor(int confirmColor) {
            this.confirmColor = confirmColor;
        }

        public boolean isSingle() {
            return isSingle;
        }

        public void setSingle(boolean single) {
            isSingle = single;
        }

        public boolean isCancelable() {
            return cancelable;
        }

        public void setCancelable(boolean cancelable) {
            this.cancelable = cancelable;
        }

        public SingleCallback getCancelCallback() {
            return cancelCallback;
        }

        public void setCancelCallback(SingleCallback cancelCallback) {
            this.cancelCallback = cancelCallback;
        }

        public SingleCallback getConfirmCallback() {
            return confirmCallback;
        }

        public void setConfirmCallback(SingleCallback confirmCallback) {
            this.confirmCallback = confirmCallback;
        }

        public OnDismissListener getDismissListener() {
            return dismissListener;
        }

        public void setDismissListener(OnDismissListener dismissListener) {
            this.dismissListener = dismissListener;
        }
    }

    public interface SingleCallback {
        void onClick(View v);
    }
}
